<?php

/**
 * Copyright              [HD框架] (C)2011-2012 houdunwang ，Inc. 
 * Encoding               UTF-8 
 * Version                $Id: rbacControl      2012-9-2 21:43:13
 * @author                向军
 * Link                   http://www.houdunwang.com       
 * E-mail                 houdunwangxj@gmail.com
 */
class rbacControl extends commonControl {

    function index() {
        $this->user_list();
    }

    //节点列表
    function node_list() {
        $nodes = $this->rbac_get_node_list();
        $this->assign("nodes", $nodes);
        $this->display();
    }

    //添加节点视图
    function node_add_show() {
        $_GET['pid'] = isset($_GET['pid']) ? $_GET['pid'] : 0;
        $_GET['level'] = isset($_GET['level']) ? $_GET['level'] : 0;
        $_GET['level']++;
        $nodename = $_GET['level'] == 1 ? "应用" : ($_GET['level'] == 2 ? "控制器" : "动作方法");
        $this->assign("nodename", $nodename);
        $this->display();
    }

    //编辑节点视图
    function node_edit_show() {
        $nid = $_GET['nid'];
        $db = M(C("NODE_TABLE"));
        $node = $db->find($nid);
        $nodename = $node['level'] == 1 ? "应用" : ($node['level'] == 2 ? "控制器" : "动作方法");
        $this->assign("nodename", $nodename);
        $this->assign("node", $node);
        $this->display();
    }

    //添加节点
    function node_add() {
        $node = M("node");
        if ($node->add()) {
            $this->success("添加" . $_POST['nodename'] . "成功", "node_list");
        } else {
            $this->error("添加" . $_POST['nodename'] . "失败", "node_list");
        }
    }

    //修改节点
    function node_edit() {
        $db = M("node");
        if ($db->save()) {
            $this->success("修改节点成功", "node_list");
        } else {
            $this->error("修改节点失败", "node_list");
        }
    }

    //删除节点
    function node_del() {
        $node = M("node");
        $nid = $_GET['nid'];
        if ($node->where("pid='$nid'")->find()) {
            $this->error("请先删除子节点", "node_list");
        } else {
            $node->del($nid);
            $this->success("节点删除成功", "node_list");
        }
    }

    //显示用户列表
    function user_list() {
        $db = K("userview");
        $page = new page($db->join()->count(), 10);
        $user = $db->all($page->limit());
        $this->assign("user", $user);
        $this->assign("page", $page->show());
        $this->display("user_list");
    }

    //添加用户视图
    function user_add_show() {
        $db = M("role");
        $role = $db->all();
        if (!$role) {
            $this->error("请先设置用户组", "role_add_show");
        }
        $this->assign("role", $role);
        $this->display();
    }

    //删除用户
    function user_del() {
        $db = K("userrelation");
        $uid = $_GET['uid'];
        if ($db->del("uid='$uid'")) {
            $this->success("删除用户成功", "user_list");
        } else {
            $this->error("删除用户失败", "user_list");
        }
    }

    //添加用户
    function user_add() {
        $db = K("userrelation");
        $_POST['password'] = md5($_POST['password']);
        if ($db->join("user_role")->add()) {
            $this->success("添加用户成功", "user_list");
        } else {
            $this->error("添加用户失败", "user_list");
        }
    }

    //暂停用户
    function user_stop() {
        $db = M("user");
        $uid = $_GET['uid'];
        if ($db->save(array("uid" => $uid, "ustate" => $_GET['ustate']))) {
            $this->success("修改成功", "user_list");
        } else {
            $this->error("修改失败", "user_list");
        }
    }

    //编辑用户
    function user_edit_show() {
        $uid = $_GET['uid'];
        $db = K("userview");
        $role = $db->table("role")->join(false)->all();
        $user = $db->where("user.uid='$uid'")->find();
        $this->assign("role", $role);
        $this->assign("user", $user);
        $this->display();
    }

    //编辑用户
    function user_edit() {
        $db = K("userrelation");
        $_POST['password'] = strlen($_POST['password']) == 32 ? $_POST['password'] : md5($_POST['password']);
        if ($db->save()) {
            $this->success("编辑用户成功", "user_list");
        } else {
            $this->error("编辑用户失败", "user_list");
        }
    }

    //用户组视图
    function role_list() {
        $db = M("role");
        $role = $db->all();
        $this->assign("role", $role);
        $this->display();
    }

    //添加用户组视图
    function role_add_show() {
        $this->display();
    }

    //添加用户组
    function role_add() {
        $db = M("role");
        if ($db->add()) {
            $this->success("添加用户组成功", "role_list");
        } else {
            $this->error("添加用户组失败", "role_list");
        }
    }

    //删除角色
    function role_del() {
        $rid = $_GET['rid'];
        $db = K("rolerelation");
        if ($db->del($rid)) {
            $this->success("删除用户组成功", "role_list");
        } else {
            $this->success("删除用户组失败", "role_list");
        }
    }

    //配置权限
    function access_set_show() {
        $rid = $_GET['rid'];
        $this->assign("rid", $rid);
        $node = $this->rbac_get_node_list($rid);
        if (!$node) {
            $this->error("请先设置权限节点", "node_add_show");
        }
        $this->assign("node", $node);
        $this->display();
    }

    //修改配置项
    function access_set() {
        $db = M(C("ACCESS_TABLE"));
        $rid = $_POST['rid'];
        $db->del("rid='$rid'");
        $nid = $_POST['nid'];
        $data = array();
        foreach ($nid as $n) {
            $arr = explode("_", $n);
            $data[] = array("rid" => $rid, "nid" => $arr[0], 'level' => $arr[1]);
        }
        if ($db->add($data)) {
            $this->success("修改权限成功", "role_list");
        } else {
            $this->success("修改权限失败", "role_list");
        }
    }

}

?>
